package com.yelobblog.yelobblog.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yelobblog.yelobblog.common.untils.Response;
import com.yelobblog.yelobblog.domain.Dto.dialogueDto;
import com.yelobblog.yelobblog.domain.Vo.dialogueGroupVo;
import com.yelobblog.yelobblog.domain.entity.agent;
import com.yelobblog.yelobblog.domain.entity.dialogue;
import com.yelobblog.yelobblog.domain.entity.dialogueGroup;
import jakarta.annotation.Resource;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;
import com.yelobblog.yelobblog.service.agentService;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;
import org.springframework.web.server.ResponseStatusException;
import java.util.Map;

@RestController
@RequestMapping("/agent")
public class agentController {
    @Resource
    private agentService agentService;

    @GetMapping("/agentList")
    public Response<Page<agent>> getAgentList(@RequestParam Integer pageNum,@RequestParam Integer pageSize){
        return Response.success(agentService.getAgentList(pageNum,pageSize));
    }
    @GetMapping("/agentByUserId")
    public Response<Page<agent>> getAgentByUserId(@RequestParam Integer userId, @RequestParam Integer pageNum, @RequestParam Integer pageSize){
        return Response.success(agentService.getAgentByUserId(userId,pageNum,pageSize));
    }
    @GetMapping("/agentById")
    public Response<agent> getAgentById(@RequestParam Integer id){
        return Response.success(agentService.getAgentById(id));
    }

    @PostMapping("/addAgent")
    public Response<String> addAgent(@RequestBody agent agent){
        agentService.addAgent(agent);
        return Response.success();
    }
    @PutMapping("/updateAgent")
    public Response<String> updateAgent(@RequestBody agent agent){
        agentService.updateAgent(agent);
        return Response.success();
    }

    @GetMapping("/dialogueGroup")
    public Response<Page<dialogueGroupVo>> getAgentById(@RequestParam Integer agentId, @RequestParam Integer pageNum, @RequestParam Integer pageSize){
        return Response.success(agentService.getDialogueGroupByAgentId(agentId,pageNum,pageSize));
    }

    @PostMapping("/addDialogueGroup")
    public Response<Integer> addDialogueGroup(@RequestBody dialogueGroup dialogueGroup){
        return Response.success(agentService.addDialogueGroup(dialogueGroup));
    }
    @DeleteMapping("/deleteDialogueGroup")
    public Response<String> deleteDialogueGroup(@RequestParam Integer id){
        agentService.deleteDialogueGroup(id);
        return Response.success();
    }
    @GetMapping("/dialogue")
    public Response<Page<dialogue>> getDialogueByGroupId(@RequestParam Integer groupId, @RequestParam Integer pageNum, @RequestParam Integer pageSize){
        return Response.success(agentService.getDialogueByGroupId(groupId,pageNum,pageSize));
    }
    @PostMapping("/addDialogue")
    public Response<String> addDialogue(@RequestBody dialogueDto dialogueDto){
        agentService.addDialogue(dialogueDto.getContent(),dialogueDto.getGroupId());
        return Response.success();
    }

    @PostMapping(value = "/ai", produces = MediaType.TEXT_EVENT_STREAM_VALUE)
    public Flux<String> ai(@RequestBody Map<String, String> request) {
        String prompt = request.get("prompt");
        Integer groupId = Integer.parseInt(request.get("groupId"));
        if (prompt == null || prompt.isEmpty()||groupId==null) {
            throw new ResponseStatusException(
                    org.springframework.http.HttpStatus.BAD_REQUEST,
                    "Prompt is required"
            );
        }
        return agentService.handleAiRequest(prompt, groupId);
    }
}
